# -*- coding = utf-8 -*-
# @Time    : 2025/3/13 下午5:00
# @Author  : yqk
# @File    : 错误与异常.py
# @Software: PyCharm


# 捕获异常
# try:
#     print("-----test---1---")
#     f = open("tes.txt", "r")
#     print("-----test---2---")
# except IOError :
#     pass


# try:
#     print("-----test---1---")
#     f = open("tes.txt", "r")
#     print("-----test---2---")
#
#     print(nam)
# except Exception as result:  # Exception是所有异常的父类
#     print("产生错误了")
#     print(result)




# try:
#     print("-----test---1---")
#     f = open("1.txt", "r")
# except Exception as result:  # Exception是所有异常的父类
#     print(result)
# finally:
#     f.close()
#     print("-----test---2---")

#
# import time
# try:
#     f = open("test2.txt", "r")
#     try:
#         while True:
#             content = f.readline()
#             if len(content) == 0:
#                 break
#             time.sleep(2)
#             print(content)
#     finally:
#         f.close()
#         print("文件关闭")
#
# except Exception as e:
#     print(e)
#     print("发生异常")


# 作业

def gus_copy():
    contents = []
    try:
        f = open("gushi.txt", "r", encoding="utf-8")
        try:
            while True:
                content = f.readline()
                if len(content) == 0:
                    break
                contents.append(content)
        finally:
            f.close()
    except Exception as e:
        print(e)
    return contents
def gus_copy2():
    try:
        f = open("copy.txt", "w",encoding="utf-8")
        try:
            content = gus_copy()
            for line in content:
                f.write(line)
        finally:
            f.close()
    except Exception as e:
        print(e)
gus_copy()
gus_copy2()






